<template>
  <div class="configure">
    <el-dialog title="配置" :visible.sync="open" width="60%" append-to-body :before-close="closeDelDialog">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="真实性" name="Authenticity">
          <el-collapse v-model="activeNames">
            <div v-for="(item, index) in Authenticity" :key="index">
              <Authenticity v-if="activeName == 'Authenticity'" :item="item" :phase="phase" :detail="detail" @handleChangeGhxx="handleChangeGhxx" :Packet="isPacket" :Signature="isSignature" @ChangeTap="ChangeTap" ref="childRef" :key="item.dId"></Authenticity>
            </div>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="完整性" name="Integrity">
          <el-collapse>
            <div v-for="(item, index) in Integrity" :key="index">
              <!-- {{ item }}--- -->
              <Integrity v-if="activeName == 'Integrity'" :item="item" :phase="phase" :detail="detail" @ChangeTap="ChangeTap"></Integrity>
            </div>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="可用性" name="Availability">
          <el-collapse>
            <div v-for="(item, index) in Availability" :key="index">
              <Availability v-if="activeName == 'Availability'" :item="item" :phase="phase" :detail="detail" @ChangeTap="ChangeTap"></Availability>
            </div>
          </el-collapse>
        </el-tab-pane>
        <el-tab-pane label="安全性" name="Security">
          <el-collapse>
            <div v-for="(item, index) in Security" :key="index">
              <Security v-if="activeName == 'Security'" :item="item" :phase="phase" :detail="detail" @ChangeTap="ChangeTap"></Security>
            </div>
          </el-collapse>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button type="primary" @click="closeDelDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getObject, saveDetectionObject} from "@/api/archive/fourNatures/detectionPlan";
import Authenticity from "@/views/archive/fourNatures/detectionPlan/authenticity";
import Integrity from "@/views/archive/fourNatures/detectionPlan/integrity";
import Availability from "@/views/archive/fourNatures/detectionPlan/availability";
import Security from "@/views/archive/fourNatures/detectionPlan/security";
export default {
  name: "configureDetail",
  components: {Security, Availability, Integrity, Authenticity },
  props: ["open"],
  data() {
    return {
      rowVal:"",
      activeName: 'Authenticity',
      activeNames: '',
      Authenticity: [],
      Integrity: [],
      Availability: [],
      Security: [],
      // 阶段
      phase: '',
      // 列表数据
      detail: {},

      // 保存数据
      parentParams: [],
      saveParams: [],
      isPacket: true,
      isSignature: true
    }
  },
  methods: {
    init(val, info) {
      // console.log(val, info)
      this.phase = info.phase
      this.detail = info
      this.getConfigure('1001', info.phase).then(item => {
        for (let i = 0; i < val.length; i++) {
          for (let j = 0; j < item.length; j++) {
            if (val[i].dId == item[j].dId) {
              val[i].pId = info.id
              val[i].categoryId = info.categoryId
              this.Authenticity.push(JSON.parse(JSON.stringify(val[i])))
            }
          }
        }
      })
      this.getConfigure('1002', info.phase).then(item => {
        this.Integrity = []
        for (let i = 0; i < val.length; i++) {
          for (let j = 0; j < item.length; j++) {
            if (val[i].dId == item[j].dId) {
              val[i].pId = info.id
              val[i].categoryId = info.categoryId
              this.Integrity.push(JSON.parse(JSON.stringify(val[i])))
            }
          }
        }
      })
      this.getConfigure('1003', info.phase).then(item => {
        this.Availability = []
        for (let i = 0; i < val.length; i++) {
          for (let j = 0; j < item.length; j++) {
            if (val[i].dId == item[j].dId) {
              val[i].pId = info.id
              val[i].categoryId = info.categoryId
              this.Availability.push(JSON.parse(JSON.stringify(val[i])))
            }
          }
        }
      })
      this.getConfigure('1004', info.phase).then(item => {
        this.Security = []
        for (let i = 0; i < val.length; i++) {
          for (let j = 0; j < item.length; j++) {
            if (val[i].dId == item[j].dId) {
              val[i].pId = info.id
              val[i].categoryId = info.categoryId
              this.Security.push(JSON.parse(JSON.stringify(val[i])))
            }
          }
        }
      })
      for (let i = 0; i < val.length; i++) {
        val[i].pId = info.id
        val[i].categoryId = info.categoryId
      }
      this.saveParams = JSON.parse(JSON.stringify(val))
    },
    getConfigure(id,code) {
      this.form = {
        id: id,
        code: code
      }
      return getObject(this.form).then(response => {
        return response.data
      })
    },
    // 关闭
    closeDelDialog() {
      this.$emit('configureCancelTap')
      this.Authenticity = []
      this.Integrity = []
      this.Availability = []
      this.Security = []
    },
    handleChangeGhxx(val, isPacket, isSignature){
       if(val == '1153') {
         this.isPacket = isPacket
       } else if(val == '1155') {
         this.isSignature = isSignature
       }
    },

    ChangeTap(item) {
      // console.log(item)
      if (this.parentParams.length == 0) {
        // 第一次
        this.parentParams.push(item);
      } else {
        let found = false;
        for (let i = 0; i < this.parentParams.length; i++) {
          if (item.dId !== this.parentParams[i].dId) {
            continue;
          }
          found = true;
          // 修改
          this.parentParams[i] = { ...item };
          break;
        }
        if (!found) {
          // push
          this.parentParams.push(item);
        }
      }
      for (let i = 0; i < this.parentParams.length; i++) {
        // if(this.parentParams[i].detectionItemList.length != 0) {
          this.saveParams.find(item => item.dId == this.parentParams[i].dId).detectionItemList = this.parentParams[i].detectionItemList
        // }
      }
    },

    // 保存配置
    submitForm() {
      console.log('this.parentParams = ', this.parentParams, ', this.saveParams = ', this.saveParams)

      for (let i = 0; i < this.saveParams.length; i++) {
        if(this.saveParams[i].isPass == 0 && this.saveParams[i].isDetection == 1) {
          if(this.saveParams[i].detectionItemList == null) {
            this.$modal.msgWarning('有必配置项'+ this.saveParams[i].dName +'未配置, 请重新配置')
            return;
          } else {
            if(this.saveParams[i].detectionItemList.length == 0){
              this.$modal.msgWarning('有必配置项'+ this.saveParams[i].dName +'未配置, 请重新配置')
              return;
            }
          }
        }
        if(this.saveParams[i].isPass == 1 || this.saveParams[i].isDetection == 0) {
          this.saveParams[i].detectionItemList = []
        }
      }
      saveDetectionObject(this.saveParams).then(response=>{
        this.$modal.msgSuccess('配置成功')
        this.$emit('configureCancelTap', 'save')
        this.Authenticity = []
        this.Integrity = []
        this.Availability = []
        this.Security = []
      })
    },
    handleClick(tab, event) {

      // console.log(this.parentParams,this.Integrity,this.Authenticity,this.Availability,this.Security,"---------------------")

    
      for (let i = 0; i < this.parentParams.length; i++) {
        // console.log(this.Authenticity.find(item => item.dId == this.parentParams[i].dId),this.parentParams[i].detectionItemList,"Authenticity")
        // console.log(this.Integrity.find(item => item.dId == this.parentParams[i].dId),this.parentParams[i].detectionItemList,"Integrity")
        // console.log(this.Availability.find(item => item.dId == this.parentParams[i].dId),this.parentParams[i].detectionItemList,"Availability")
        // console.log(this.Security.find(item => item.dId == this.parentParams[i].dId),this.parentParams[i].detectionItemList,"Security")
        if(this.Authenticity.find(item => item.dId == this.parentParams[i].dId) != undefined) {
          this.Authenticity.find(item => item.dId == this.parentParams[i].dId).detectionItemList = this.parentParams[i].detectionItemList
        }
        if(this.Integrity.find(item => item.dId == this.parentParams[i].dId) != undefined) {
       
          this.Integrity.find(item => item.dId == this.parentParams[i].dId).detectionItemList = this.parentParams[i].detectionItemList
        }
        if(this.Availability.find(item => item.dId == this.parentParams[i].dId) != undefined) {
         
          this.Availability.find(item => item.dId == this.parentParams[i].dId).detectionItemList = this.parentParams[i].detectionItemList
        }
        if(this.Security.find(item => item.dId == this.parentParams[i].dId) != undefined) {
          this.Security.find(item => item.dId == this.parentParams[i].dId).detectionItemList = this.parentParams[i].detectionItemList
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
